
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/account/iview/below_user_deposit_score_list_iview.dart';
import 'package:flutter_deer/account/iview/below_user_takeout_score_list_iview.dart';
import 'package:flutter_deer/account/models/user_score_deposit_entity.dart';
import 'package:flutter_deer/account/models/user_score_takeout_entity.dart';
import 'package:flutter_deer/account/presenter/below_user_deposit_score_list_presenter.dart';
import 'package:flutter_deer/account/presenter/below_user_takeout_score_list_presenter.dart';
import 'package:flutter_deer/account/provider/below_user_score_list_provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/res/gaps.dart';
import 'package:flutter_deer/util/image_utils.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/my_card.dart';
import 'package:flutter_deer/widgets/my_refresh_list.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';

class BelowUserScoreListListPage extends StatefulWidget {

  final int index;
  final String username;
  const BelowUserScoreListListPage({super.key,required this.index,required this.username});

  @override
  _BelowUserScoreListPageState createState() => _BelowUserScoreListPageState();
}

class _BelowUserScoreListPageState extends State<BelowUserScoreListListPage> with BasePageMixin<BelowUserScoreListListPage, PowerPresenter>, AutomaticKeepAliveClientMixin<BelowUserScoreListListPage>,SingleTickerProviderStateMixin implements BelowUserDepositScoreListIMvpView,BelowUserTakeOutScoreListIMvpView {

  late BelowUserDepositScoreListPresenter belowUserDepositScoreListPresenter;
  late BelowUserTakeOutScoreListPresenter belowUserTakeOutScoreListPresenter;
  BelowUserScoreListProvider provider = BelowUserScoreListProvider();

  @override
  void setBelowUserDepositScorePage(BelowUserScoreDepositPageEntity? pageEntity){
    provider.setBelowUserDepositScorePage(pageEntity);
  }

  @override
  void setBelowUserTakeOutScorePage(BelowUserScoreTakeoutPageEntity? pageEntity){
    provider.setBelowUserTakeOutScorePage(pageEntity);
  }

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(
        this);
    belowUserDepositScoreListPresenter =
    new BelowUserDepositScoreListPresenter();
    belowUserTakeOutScoreListPresenter =
    new BelowUserTakeOutScoreListPresenter();
    powerPresenter.requestPresenter([
      belowUserDepositScoreListPresenter,
      belowUserTakeOutScoreListPresenter
    ]);
    return powerPresenter;
  }


  @override
  void initState() {
    super.initState();
    if(mounted){
      _onRefresh();
    }
  }

  @override
  void dispose() {
    // provider.dispose();
    super.dispose();
  }

  Future<dynamic> _onRefresh() async {
    Future.delayed(Duration(seconds: 1),(){
      if(widget.index==0) {
        belowUserDepositScoreListPresenter.getDepositScoreList(widget.username);
      }else if(widget.index==1){
        belowUserTakeOutScoreListPresenter.getTakeOutScoreList(widget.username);
      }
    });

  }

  Future<dynamic> _loadMore() async {
    if(widget.index==0) {
      belowUserDepositScoreListPresenter.getMoreDepositScoreList(widget.username,provider.currentPageDeposit!+1);
    }else if(widget.index==1){
      belowUserTakeOutScoreListPresenter.getMoreTakeOutScoreList(widget.username,provider.currentPageTakeOut!+1);
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ChangeNotifierProvider<BelowUserScoreListProvider>(
        create: (_) => provider,
        child: Consumer<BelowUserScoreListProvider>(
        builder: (_, provider, child) {
      if(widget.index==0){
        if(provider.depositStateType==StateType.loading){
          return StateLayout(type: provider.depositStateType!);
        }
        return DeerListView(
            itemCount: provider.depositList!.length!,
            stateType: provider.depositStateType!,
            onRefresh: _onRefresh,
            loadMore: _loadMore,
            hasMore: provider.hasDepositMore!,
            itemBuilder: (_, index) {
              final BelowUserScoreDepositEntity item = provider.depositList!
                  .elementAt(index);
              return Padding(
                  padding: EdgeInsets.symmetric(horizontal: 10.0,vertical: 5),
                  child: MyCard(
                    child: Padding(
                      padding: EdgeInsets.symmetric(horizontal: 5),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Gaps.vGap5,
                          Row(
                              children: <Widget>[
                                CircleAvatar(radius: 24.0,backgroundColor: Colors.transparent,backgroundImage: ImageUtils.getImageProvider(Constant.imageServerHost+'${item.avatar}', holderImg: 'shop/tx')),
                                Gaps.hGap8,
                                Text('${DeerLocalizations.of(context)!.playerAccount.nullSafe}:${item.accountId}')
                              ]
                          ),
                          Gaps.vGap8,
                          Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              Text('${DeerLocalizations.of(context)!.rechargeAmount.nullSafe}:'+NumberFormat.decimalPattern('en_us').format(item.score).toString()),
                              Text('${DeerLocalizations.of(context)!.rechargeTime.nullSafe}:'+item.updateTime.toString())
                            ]
                          ),
                          Gaps.vGap5
                        ]
                      ),
                    )
                  )
              );
            }
        );
      }else {
        if (provider.takeOutStateType == StateType.loading) {
          return StateLayout(type: provider.takeOutStateType!);
        }
        return DeerListView(
            itemCount: provider.takeOutList!.length!,
            stateType: provider.takeOutStateType!,
            onRefresh: _onRefresh,
            loadMore: _loadMore,
            hasMore: provider.hasTakeOutMore!,
            itemBuilder: (_, index) {
              final BelowUserScoreTakeoutEntity item = provider.takeOutList!
                  .elementAt(index);
              return Padding(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 10.0, vertical: 5),
                  child: MyCard(
                      child: Padding(
                        padding: const EdgeInsets.symmetric(horizontal: 5),
                        child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Gaps.vGap5,
                              Row(
                                  children: <Widget>[
                                    CircleAvatar(radius: 24.0,backgroundColor: Colors.transparent,backgroundImage: ImageUtils.getImageProvider(Constant.imageServerHost+'${item.avatar}', holderImg: 'shop/tx')),
                                    Gaps.hGap8,
                                    Text('${DeerLocalizations.of(context)!.playerAccount.nullSafe}:${item.accountId}')
                                  ]
                              ),
                              Gaps.vGap8,
                              Row(
                                  mainAxisAlignment: MainAxisAlignment
                                      .spaceBetween,
                                  children: [
                                    Text('${DeerLocalizations.of(context)!.withdrawAmount.nullSafe}:' + NumberFormat.decimalPattern('en_us').format(item.score).toString()),
                                    Text('${DeerLocalizations.of(context)!.withdrawTimes.nullSafe}:' + item.updateTime.toString())
                                  ]
                              ),
                              Gaps.vGap5
                            ]
                        ),
                      )
                  )
              );
            }
        );
      }
      }
    ));
}

  @override
  bool get wantKeepAlive => false;
}